package com.gongpingjialoan.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.gongpingjialoan.R;

public class CarCheckBox extends ImageView 
{
    
    boolean checked = false;
    
    OnCheckChangeListener onCheckChangeListener;
    
    public boolean isChecked()
    {
        return checked;
    }
    
    public OnCheckChangeListener getOnCheckChangeListener()
    {
        return onCheckChangeListener;
    }
    
    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener)
    {
        this.onCheckChangeListener = onCheckChangeListener;
    }
    
    public CarCheckBox(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setChecked(false);
    }
    
    public CarCheckBox(Context context)
    {
        super(context);
        setChecked(false);
        this.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (onCheckChangeListener == null)
                {
                    setChecked(!checked);
                }
                else if (onCheckChangeListener != null)
                {
                    if (!isChecked())
                    {
                        setChecked(true);
                        onCheckChangeListener.onChange(CarCheckBox.this, true);
                    }
                    else
                    {
                        setChecked(false);
                        onCheckChangeListener.onChange(CarCheckBox.this, false);
                    }
                }
            }
        });
    }
    
    
    public void addOnCheckChangeListener()
    {
        
    }
    
    
    public void ScaleOutAnimation()
    {
        ScaleAnimation animation =
            new ScaleAnimation(1f, 1.5f, 1f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);// 设置动画持续时间
        /** 常用方法 */                                                                           
        // animation.setRepeatCount(int repeatCount);//设置重复次数
         animation.setFillAfter(false);//动画执行完后是否停留在执行完的状态
        
//        ScaleAnimation myAnimation_Scale =
//            new ScaleAnimation(0.1f, 1.0f, 0.1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//        myAnimation_Scale.setFillAfter(false);
//        myAnimation_Scale.setInterpolator(new AccelerateInterpolator());
//        AnimationSet aa = new AnimationSet(true);
//        aa.addAnimation(myAnimation_Scale);
//        aa.setDuration(500);
        
        this.startAnimation(animation);
    }
    

    
    public void setChecked(boolean checked)
    {
        this.checked = checked;
        invalidate();
        if (checked)
        {
            setImageResource(R.drawable.car_point_f);
            ScaleOutAnimation();
        }
        else
        {
            setImageResource(R.drawable.car_point_n);
        }
    }
    
    public interface OnCheckChangeListener 
    {
        void onChange(View v, boolean checked);
    }
}
